package com.easyadmin.controller;

import com.easyadmin.annotation.GlobalInterceptor;
import com.easyadmin.annotation.VerifyParam;
import com.easyadmin.entity.enums.OperModuleEnum;
import com.easyadmin.entity.enums.PermissionCodeEnum;
import com.easyadmin.entity.po.SysDict;
import com.easyadmin.entity.query.SysDictQuery;
import com.easyadmin.entity.vo.AjaxResponseVO;
import com.easyadmin.entity.vo.PaginationResult;
import com.easyadmin.exception.BusinessException;
import com.easyadmin.service.SysDictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/settings")
public class SysDicController extends ABaseController {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private SysDictService sysDictService;


    /**
     * @Description: 获取字典列表
     * @auther: 多多洛
     * @date: 15:33 2020/9/25
     * @param: [query]
     * @return: AjaxResponseVO
     */
    @RequestMapping("loadDictList")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.SETTINGS_DIC)
    public AjaxResponseVO loadDictList(SysDictQuery query) {
        try {
            query.setPid(0);
            query.setOrderBy("sort asc");
            query.setQueryChildren(true);
            PaginationResult<SysDict> sysDicList = sysDictService.findListByPage(query);
            return getSuccessResponseVO(sysDicList);
        } catch (Exception e) {
            logger.error("获取字典列表异常", e);
            return getServerErrorResponseVO();
        }
    }

    /**
     * @Description: 保存字典
     * @auther: 多多洛
     * @date: 15:35 2020/9/25
     * @param: [sysDict]
     * @return: AjaxResponseVO
     */
    @RequestMapping("saveDict")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.SETTINGS_DIC_EDIT, opObject = OperModuleEnum.OBJECT_DIC, opDescription = "保存字典，请求参数:#{#param0}")
    public AjaxResponseVO saveDict(@RequestBody SysDict sysDict) {
        try {
            sysDictService.saveDict(sysDict);
            return getSuccessResponseVO(null);
        } catch (BusinessException e) {
            logger.error("保存字典列表异常", e);
            return getBusinessErrorResponseVO(e);
        } catch (Exception e) {
            logger.error("保存字典列表异常", e);
            return getServerErrorResponseVO();
        }
    }


    @RequestMapping("getDict")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.SETTINGS_DIC)
    public AjaxResponseVO getDict(Integer id) {
        try {
            SysDict sysDict = sysDictService.getSysDictById(id);
            if (sysDict != null) {
                SysDictQuery dictQuery = new SysDictQuery();
                dictQuery.setPid(sysDict.getId());
                dictQuery.setOrderBy("sort asc");
                sysDict.setSubData(sysDictService.findListByParam(dictQuery));
            }
            return getSuccessResponseVO(sysDict);
        } catch (Exception e) {
            logger.error("获取字典列表异常", e);
            return getServerErrorResponseVO();
        }
    }


    @RequestMapping("delDict")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.SETTINGS_DIC_DEL, opObject = OperModuleEnum.OBJECT_DIC, opDescription = "删除字典Id:#{#param0}")
    public AjaxResponseVO delDict(@VerifyParam(required = true) Integer id) {
        try {
            sysDictService.deleteById(id);
            return getSuccessResponseVO(null);
        } catch (Exception e) {
            logger.error("获取字典列表异常", e);
            return getServerErrorResponseVO();
        }
    }

    @RequestMapping("loadSubDictList")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.SETTINGS_DIC)
    public AjaxResponseVO loadSubDictList(@VerifyParam(required = true) String propValue) {
        try {
            SysDict sysDict = sysDictService.getSysDictByPropValueAndPid(propValue, 0);
            if (sysDict == null) {
                throw new BusinessException("字典值不存在");
            }
            SysDictQuery sysDictQuery = new SysDictQuery();
            sysDictQuery.setOrderBy("sort asc");
            sysDictQuery.setPid(sysDict.getId());
            List<SysDict> sysDictList = sysDictService.findListByParam(sysDictQuery);
            return getSuccessResponseVO(sysDictList);
        } catch (BusinessException e) {
            logger.error("获取字典列表异常", e);
            return getBusinessErrorResponseVO(e);
        } catch (Exception e) {
            logger.error("获取字典列表异常", e);
            return getServerErrorResponseVO();
        }
    }
}
